package com.czmh.etc.onlinepubsys.exceptions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.czmh.etc.onlinepubsys.constants.GlobalStatusCode;
import com.czmh.etc.onlinepubsys.json.JsonResult;

/**
 * @描述: Spring异常拦截器.
 * @作者: Seven .
 * @创建时间: 2016年12月16日-下午15:14:56.
 * @版本: 1.0 .
 */
@ControllerAdvice
public class WebExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(WebExceptionHandler.class);

    @ExceptionHandler({ Exception.class })
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public JsonResult processException(Exception e, HttpServletRequest request) {
        e.printStackTrace();
        logger.error("Exception", e);
        JsonResult json = new JsonResult();
        json.setCode(GlobalStatusCode.FAILED);
        json.setMessage(GlobalStatusCode.EXCEPTION);
        return json;
    }

    @ExceptionHandler({ EtcInterpreterException.class })
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public JsonResult processEtcInterpreterException(EtcInterpreterException e, HttpServletRequest request) {
        logger.error("EtcInterpreterException", e);
        JsonResult json = new JsonResult();
        json.setCode(e.getCode());
        json.setMessage(e.getMsg());
        return json;
    }

    @ExceptionHandler({ RuntimeException.class })
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public JsonResult processRuntimeException(RuntimeException e, HttpServletRequest request) {
        logger.error("RuntimeException", e);
        JsonResult json = new JsonResult();
        json.setCode(JsonResult.CODE_FAUILE);
        json.setMessage(e.getMessage());
        return json;
    }

    @ExceptionHandler({ EtcMsgException.class })
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public JsonResult processEtcMsgException(EtcMsgException e, HttpServletRequest request) {
        logger.error("EtcMsgException", e);
        JsonResult json = new JsonResult();
        json.setCode(JsonResult.CODE_SUCCESS);
        json.setMessage(e.getMessage());
        return json;
    }

    @ExceptionHandler({ MethodArgumentNotValidException.class })
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public JsonResult processMethodArgumentNotValidException(MethodArgumentNotValidException e,
            HttpServletRequest request) {
        logger.error("MethodArgumentNotValidException", e);
        JsonResult json = new JsonResult();
        BindingResult bindingResult = e.getBindingResult();
        List<ObjectError> errors = bindingResult.getAllErrors();
        json.setCode(JsonResult.CODE_SUCCESS);
        json.setMessage(errors.get(0).getDefaultMessage());
        return json;
    }
}
